﻿//   -------------------- iSharePoint -------------------- //
//  
//   Company:	IT Complex, LLC
//   Project:	Smart.Common
//   File name:	SPResources.cs
//   Developer:	Игорь Соломатов
//   Created:	28.08.2012
//  
//   -------------------- iSharePoint -------------------- //

using System;
using System.ComponentModel;
using System.Threading;

namespace Smart.Common
{
	[Localizable(false)]
	public class SPResources
	{
		public string ResourceKey;
		public SyncDictionary<string, string> LocalizationCache;

		public SPResources(string resourceKey)
		{
			ResourceKey = resourceKey;
			LocalizationCache = new SyncDictionary<string, string>(StringComparer.OrdinalIgnoreCase);
		}

		public uint GetLang()
		{
			return (uint)Thread.CurrentThread.CurrentUICulture.LCID;
		}

		public string GetLocalizedString(string key)
		{
			if (string.IsNullOrEmpty(key))
				return key;

			var lang = GetLang();

			return
				LocalizationCache.SafeGetLoad(
					String.Concat(key, ":", lang),
					delegate
					{
						var res = Microsoft.SharePoint.Utilities.SPUtility.GetLocalizedString(String.Concat("$Resources:", ResourceKey, ",", key, ";"), ResourceKey, lang);

						if (res != null && res.StartsWith("$Resources", StringComparison.OrdinalIgnoreCase))
							H.LT("Resource not found: {0}", key);

						return res;
					});
		}
	}
}